<!--
 * @description: 登录表单
 * @Author: Islands
 * @Date: 2024-06-15 03:18:01
 * @LastEditTime: 2024-06-21 10:44:59
-->
<script lang="ts" setup>
import { ref, reactive, toRefs } from "vue";
import { $t } from "@/plugins/i18n/index";
import { User, Lock } from "@element-plus/icons-vue";
import type { FormInstance, FormRules } from "element-plus";
import { ILoginData } from "@/api/types/login.d";
import { useCountDown } from "@/hooks";
const { countdown, countDown } = useCountDown();

const ruleFormRef = ref<FormInstance>();
const { loginForm, rulesForm } = toRefs(
  reactive({
    loginForm: {} as ILoginData,
    rulesForm: {
      username: [
        {
          required: true,
          message: $t(`placeholder.login.phone`, {
            prefix: $t(`placeholder.enter`),
          }),
          trigger: "click",
        },
      ],
      password: [
        {
          required: true,
          message: $t(`placeholder.login.code`, {
            prefix: $t(`placeholder.enter`),
          }),
          trigger: "click",
        },
      ],
    } as FormRules,
  })
);

/**
 * 登录方法
 * @param formEl
 * @param loginForm
 */
const loginSubmit = async (
  formEl: FormInstance | undefined,
  _loginForm: ILoginData
) => {
  if (!formEl) return;
  await formEl.validate((valid) => {
    if (valid) {
      //执行登录方法
      console.log("submit!");
    }
  });
};

</script>
<template>
  <el-form ref="ruleFormRef" :model="loginForm" :rules="rulesForm" size="large">
    <el-form-item prop="username">
      <el-input
        :prefix-icon="User"
        v-model="loginForm.username"
        :placeholder="$t(`placeholder.login.phone`)"
      />
    </el-form-item>
    <el-form-item prop="code">
      <div class="between-space-justify" style="width: 100%">
        <el-input
          style="width: 65%"
          :prefix-icon="Lock"
          v-model="loginForm.code"
          :placeholder="$t(`placeholder.login.code`)"
        />
        <el-button :disabled="countdown > 0" @click="countDown(60)">
          {{
            countdown > 0
              ? `${countdown}` + $t(`title.timerAgain`)
              : $t(`button.getCode`)
          }}
        </el-button>
      </div>
    </el-form-item>
    <el-form-item>
      <el-button
        style="width: 100%"
        type="success"
        auto-insert-space
        dark
        round
        @click="loginSubmit(ruleFormRef, loginForm)"
        >{{ $t(`button.login`) }}</el-button
      >
    </el-form-item>
  </el-form>
</template>
